package com.jm.offer.easy;

/**
 * @Description https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/
 * @date 2022/4/9 7:55
 */
public class _15二进制中1的个数 {

    public class Solution {
        public int hammingWeight(int n) {
            int ret = 0;
            while (n != 0) {
                // 最低位的1会被反转成0，直到全为0
                // n = 110 & 101 = 100  ret = 1;
                // n = 100 & 010 = 000  ret = 2;
                // n = 0 退出。
                n &= n - 1;
                ret++;
            }
            return ret;
        }
    }
}
